package org.smardi.fakeCall.CLIQr;

import org.smardi.fakeCall.*;
import org.smardi.fakeCall.Service.*;
import org.smardi.fakeCall.Setting.*;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class AC_CLIQr extends Activity {

	TextView btn_back = null;
	
	//CLIQ.r 사용 여부 확인
	CheckBox cbox_cliqr = null;
	
	Button btn_cliqr_register = null;
	Button btn_cliqr_test = null;
	
	Button btn_apply = null;
	
	SharedPreference_FakeCall mPref = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_cliqr);
		
		initComponent();
	}

	private void initComponent() {
		
		mPref = new SharedPreference_FakeCall(AC_CLIQr.this);
		
		btn_back = (TextView)findViewById(R.id.btn_top_left);
		
		cbox_cliqr = (CheckBox)findViewById(R.id.cbox_cliqr);
		cbox_cliqr.setChecked(mPref.isCLIQrMode());
		
		btn_cliqr_register = (Button)findViewById(R.id.btn_cliqr_register);
		btn_cliqr_test = (Button)findViewById(R.id.btn_cliqr_test);
		
		btn_apply = (Button)findViewById(R.id.btn_apply);
		
		
		registEvent();
	}
	
	
	private void registEvent() {
		btn_back.setOnClickListener(viewClickListener);
		
		btn_cliqr_register.setOnClickListener(viewClickListener);
		btn_cliqr_test.setOnClickListener(viewClickListener);
		
		btn_apply.setOnClickListener(viewClickListener);
		
		cbox_cliqr.setOnClickListener(viewClickListener);
	}


	View.OnClickListener viewClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch(v.getId()) {
			case R.id.btn_top_left:
				finish();
				break;
				//--------------------------------
			case R.id.cbox_cliqr:
				checkCLIQr_Registrated();
				break;
				//--------------------------------
			case R.id.btn_cliqr_register:
				startActivity(new Intent(AC_CLIQr.this, AC_Dialog_CLIQr_Register.class));
				break;
			case R.id.btn_cliqr_test:
				if(checkCLIQr_Registrated() == true) {
					startActivity(new Intent(AC_CLIQr.this, AC_Dialog_CLIQr_Test.class));
				}
				break;
			case R.id.btn_apply:
				apply();
				finish();
				break;
			}
		}
	};
	
	private boolean checkCLIQr_Registrated() {
		int CLIQr_freq = mPref.getCLIQrFreq();
		if(CLIQr_freq <= 0) {
			//클리커 등록이 안되어있다는 말
			Toast.makeText(AC_CLIQr.this, "CLIQ.r 신호 등록을 먼저 해 주세요", 1000).show();
			cbox_cliqr.setChecked(false);
			
			return false;
		}
		return true;
	}
	
	private void apply() {
		if(cbox_cliqr.isChecked() == true) {
			mPref.setCLIQrMode(true);
			sendBroadcast(new Intent(SV_FakeCall.ACTION_FAKECALL_CLIQ_ON));
		} else {
			mPref.setCLIQrMode(false);
			sendBroadcast(new Intent(SV_FakeCall.ACTION_FAKECALL_CLIQR_CANCEL));
		}
		
	}
}
